<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/3
 * Time: 14:59
 */
namespace app\admin\controller;
use app\data\Data;
use app\validate\ShareValidate;
use think\Config;
use think\Request;

/**
 * +--------------------------------------
 * Class Link
 * @package app\admin\controller
 * 源码分享管理
 * +-------------------------------------
 */
class Share extends Check{
    /**
     * 分享列表
     */
    public function shareList(){
        $link_model = new Data('product');
        $link_name = input('git_url');
        $page = input('page');
        if(!empty($link_name)){
            $where['git_url'] = ['like','%'.$link_name.'%'];
            $display['git_name'] = $link_name;
        }
        $result = $link_model->pageSelf($where,'','',$display,'',$page);
        return view('shareList',['result'=>$result,'display'=>$display]);
    }

    /**
     * 添加源码分享
     */
    public function shareAdd(){
        $share_model = new Data('product');
        $share_validate = new ShareValidate();
        try{
            if(Request::instance()->isPost()){
                $post = input('param.');
                $res = $share_model->selectData("git_url = '".trim($post['git_url'])."'");
                if($res){
                    throw new \LogicException(Config::get('error_table.link_url')['msg'],Config::get('error_table.link_url')['code']);
                }
                $link_validate = $share_validate->linkValidate();
                self::validateData($link_validate['rule'],$link_validate['message'],$post);
                $post['create_time'] = time();
                $rs = $share_model->insertData($post);
                if(!$rs){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('shareAdd');
    }
    /**
     * 更新源码分享
     */
    public function shareSave(){
        $share_model = new Data('product');
        $share_validate = new ShareValidate();
        try{
            $id = intval(input('id'));
            if(Request::instance()->isPost()){
                $post = input('param.');
                $link_validate = $share_validate->linkValidate();
                self::validateData($link_validate['rule'],$link_validate['message'],$post);
                $rs = $share_model->updateData($post);
                if($rs === false){
                    throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
                }
                return self::formatSuccessResult();
            }
            $result = $share_model->find('id ='.$id);
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('shareSave',['result'=>$result]);
    }

    /**
     * 删除分享
     */
    public function shareDelete(){
        $share_model = new Data('product');
        $id = intval(input('id'));
        try{
            $rs = $share_model->deleteDaPtaByPrimaryKey($id);
            if($rs === false){
                throw new \LogicException(Config::get('error_table.error')['msg'],Config::get('error_table.error')['code']);
            }
            return self::formatSuccessResult();
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
    }
}
